package com.drug.wms.service.impl;

import com.drug.common.utils.DateUtils;
import com.drug.common.utils.SecurityUtils;
import com.drug.wms.domain.BusGoodsStockDetail;
import com.drug.wms.domain.BusGoodsStockDetailList;
import com.drug.wms.domain.BusGoodsStockInfo;
import com.drug.wms.mapper.BusGoodsStockDetailMapper;
import com.drug.wms.mapper.BusGoodsStockInfoMapper;
import com.drug.wms.service.IBusGoodsStockDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品入库清单详情Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class BusGoodsStockDetailServiceImpl implements IBusGoodsStockDetailService {
    @Autowired
    private BusGoodsStockDetailMapper busGoodsStockDetailMapper;
    @Autowired
    private BusGoodsStockInfoMapper busGoodsStockInfoMapper;

    /**
     * 查询商品入库清单详情
     *
     * @param id 商品入库清单详情主键
     * @return 商品入库清单详情
     */
    @Override
    public BusGoodsStockDetail selectBusGoodsStockDetailById(String id) {
        return busGoodsStockDetailMapper.selectBusGoodsStockDetailById(id);
    }

    /**
     * 查询商品最近入库信息
     *
     * @param goodsNo 商品编号
     * @return 商品入库清单详情
     */
    @Override
    public BusGoodsStockDetail selectBusGoodsStockDetailLatestByGoodsNo(String goodsNo) {
        return busGoodsStockDetailMapper.selectBusGoodsStockDetailLatest(goodsNo);
    }

    /**
     * 查询商品入库清单详情列表
     *
     * @param busGoodsStockDetail 商品入库清单详情
     * @return 商品入库清单详情
     */
    @Override
    public List<BusGoodsStockDetail> selectBusGoodsStockDetailList(BusGoodsStockDetail busGoodsStockDetail) {
        return busGoodsStockDetailMapper.selectBusGoodsStockDetailList(busGoodsStockDetail);
    }

    @Override
    public List<BusGoodsStockDetail> selectBusGoodsStockDetailListByOr(BusGoodsStockDetail busGoodsStockDetail) {
        return busGoodsStockDetailMapper.selectBusGoodsStockDetailListByOr(busGoodsStockDetail);
    }

    /**
     * 新增商品入库清单详情
     *
     * @param busGoodsStockDetail 商品入库清单详情
     * @return 结果
     */
    @Override
    public int insertBusGoodsStockDetail(BusGoodsStockDetail busGoodsStockDetail) {
        assert busGoodsStockDetail != null;
        BusGoodsStockInfo   busGoodsStockInfo = busGoodsStockInfoMapper.selectBusGoodsStockInfoById(busGoodsStockDetail.getStockId());
        busGoodsStockDetail.setReceivingNo(busGoodsStockInfo.getReceivingWarehouse());
        busGoodsStockDetail.setCreateTime(DateUtils.getNowDate());
        return busGoodsStockDetailMapper.insertBusGoodsStockDetail(busGoodsStockDetail);
    }

    /**
     * 新增商品入库清单详情
     *
     * @param busGoodsStockDetailList 商品入库清单详情
     * @return 结果
     */
    @Override
    public int insertBusGoodsStockDetail(BusGoodsStockDetailList busGoodsStockDetailList) {
        BusGoodsStockInfo busGoodsStockInfo = null;
        //获取当前单据收货单位
        if (busGoodsStockDetailList != null && StringUtils.isNotEmpty(busGoodsStockDetailList.getId())) {
            busGoodsStockInfo = busGoodsStockInfoMapper.selectBusGoodsStockInfoById(busGoodsStockDetailList.getId());
        }
        assert busGoodsStockDetailList != null;
        for (BusGoodsStockDetail b : busGoodsStockDetailList.getDetailList()) {
            if (busGoodsStockInfo != null) {
                b.setReceivingNo(busGoodsStockInfo.getReceivingWarehouse());
            }
            b.setStockId(busGoodsStockDetailList.getId());
            busGoodsStockDetailMapper.insertBusGoodsStockDetail(b);
        }
        return 1;
    }

    /**
     * 批量新增入库药品
     *
     * @param busGoodsStockDetail
     * @return
     */
    @Override
    @Transactional
    public int insertBusGoodsStockDetail(BusGoodsStockDetail[] busGoodsStockDetail) {
        BusGoodsStockInfo busGoodsStockInfo = null;
        //获取当前单据收货单位
        if (busGoodsStockDetail != null && busGoodsStockDetail.length != 0) {
            busGoodsStockInfo = busGoodsStockInfoMapper.selectBusGoodsStockInfoById(busGoodsStockDetail[0].getStockId());
        }
        assert busGoodsStockDetail != null;
        for (BusGoodsStockDetail b : busGoodsStockDetail) {
            b.setReceivingNo(busGoodsStockInfo.getReceivingWarehouse());
            busGoodsStockDetailMapper.insertBusGoodsStockDetail(b);
        }
        return 1;
    }

    /**
     * 修改商品入库清单详情
     *
     * @param busGoodsStockDetail 商品入库清单详情
     * @return 结果
     */
    @Override
    public int updateBusGoodsStockDetail(BusGoodsStockDetail busGoodsStockDetail) {
        busGoodsStockDetail.setUpdateTime(DateUtils.getNowDate());
        return busGoodsStockDetailMapper.updateBusGoodsStockDetail(busGoodsStockDetail);
    }

    /**
     * 批量修改商品入库清单详情
     *
     * @param busGoodsStockDetail 商品入库清单详情
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusGoodsStockDetail(BusGoodsStockDetail[] busGoodsStockDetail) {
        for (BusGoodsStockDetail b : busGoodsStockDetail) {
            if (StringUtils.isEmpty(b.getId())) {
                busGoodsStockDetailMapper.insertBusGoodsStockDetail(b);
            } else {
                busGoodsStockDetailMapper.updateBusGoodsStockDetail(b);
            }
        }
        return 1;
    }

    /**
     * 批量删除商品入库清单详情
     *
     * @param ids 需要删除的商品入库清单详情主键
     * @return 结果
     */
    @Override
    public int deleteBusGoodsStockDetailByIds(String[] ids) {
        return busGoodsStockDetailMapper.deleteBusGoodsStockDetailByIds(ids);
    }

    /**
     * 删除商品入库清单详情信息
     *
     * @param id 商品入库清单详情主键
     * @return 结果
     */
    @Override
    public int deleteBusGoodsStockDetailById(String id) {
        return busGoodsStockDetailMapper.deleteBusGoodsStockDetailById(id);
    }
}
